package org.csu.jpetstore_users_backend_pj.controller;


import org.csu.jpetstore_users_backend_pj.entity.Product;
import org.csu.jpetstore_users_backend_pj.result.CommonResponse;
import org.csu.jpetstore_users_backend_pj.result.ResponseCode;
import org.csu.jpetstore_users_backend_pj.service.CatalogService;
import org.csu.jpetstore_users_backend_pj.vo.*;
import org.csu.jpetstore_users_backend_pj.vo.CategoryVO;
import org.csu.jpetstore_users_backend_pj.vo.InfoWindowCategoryVO;
import org.csu.jpetstore_users_backend_pj.vo.ItemVO;
import org.csu.jpetstore_users_backend_pj.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@CrossOrigin(origins = "http://localhost:5173")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("/index")
    public String index(){
        return "catalog/main";
    }

    @GetMapping("/categories/{categoryId}/products")
    public CommonResponse viewCategory(@PathVariable String categoryId){
        CategoryVO categoryVO = catalogService.getCategory(categoryId);
        return CommonResponse.createForSuccess(ResponseCode.GetCategoryVOSuccess.getCode(),ResponseCode.GetCategoryVOSuccess.getDescription(),categoryVO);
    }

    @GetMapping("/categories")
    public CommonResponse<List<CategoriesVO>> viewCategories() {
        List<CategoriesVO> categories = catalogService.getCategories();
        return CommonResponse.createForSuccess(ResponseCode.GetCategoriesVOSuccess.getCode(),ResponseCode.GetCategoriesVOSuccess.getDescription(),categories);
    }

    @GetMapping("/products/{productId}/items")
    public CommonResponse viewProduct(@PathVariable String productId){
        ProductVO productVO = catalogService.getProduct(productId);
        return CommonResponse.createForSuccess(ResponseCode.GetProductVOSuccess.getCode(),ResponseCode.GetProductVOSuccess.getDescription(),productVO);
    }

    @GetMapping("/items/{itemId}")
    public CommonResponse viewItem(@PathVariable String itemId){
        System.out.println(itemId);
        ItemVO itemVO = catalogService.getItem(itemId);
        return CommonResponse.createForSuccess(ResponseCode.GetItemVOSuccess.getCode(),ResponseCode.GetItemVOSuccess.getDescription(),itemVO);
    }

    //搜索
    @GetMapping("/catalog/productAuto")
    public CommonResponse<List<Product>> productAuto(@RequestParam String keyword) {
        List<Product> productList = catalogService.searchProductList(keyword);
        return CommonResponse.createForSuccess(ResponseCode.GetSearchMessageSuccess.getCode(),ResponseCode.GetSearchMessageSuccess.getDescription(),productList);
    }

    @GetMapping("/infoWindowCategory/{categoryId}")
    public CommonResponse infoWindowCategory(@PathVariable String categoryId) {
        InfoWindowCategoryVO infoWindowCategoryVO = catalogService.getInfoWindowCategoryVO(categoryId);
        return CommonResponse.createForSuccess(ResponseCode.GetInfoWindowCategorySuccess.getCode(),ResponseCode.GetSearchMessageSuccess.getDescription(),infoWindowCategoryVO);
    }

}

